package com.gxuwz.attendance.dao;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import com.gxuwz.attendance.entity.Record;
import com.gxuwz.attendance.entity.User;
import com.gxuwz.attendance.vo.Page;
import net.sf.json.JSONArray;

@Repository("RecordDao")
public class RecordDao extends SessionFactoryDao{

	/**
	 * 更新考勤记录
	 * @param record
	 * @return
	 */
	public boolean update(Record record) {
		boolean flag = true;
		Session session = null;
		try {
			session = getSession();
			// 开启事务
			session.beginTransaction();
			session.update(record);
			session.getTransaction().commit();
		}catch (Exception e) {
			// 出现异常更新失败
			flag = false;
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		
		return flag;
	}
	
	/**
	 * 批量保存考勤记录
	 * @param data
	 */
	public void batchSave(String data) {
		JSONArray array = JSONArray.fromObject(data);
		Session session = null;
		try {
			session = getSession();
			// 开启事务
			session.beginTransaction();
			for (int i = 0; i < array.size(); i++) {
				Record record = new Record();
				
				String hql = "from Record  where studentNo = '" + array.getJSONObject(i).getString("studentNo") + "'";

				int late = 0;
				int early = 0;
				int normal = 0;
				int truancy = 0;
				int leave = 0;
				
				boolean flag = false;
				
				if(getSession().createQuery(hql).list().isEmpty()) {
					record = new Record();
					late = 0;
					early = 0;
					normal = 0;
					truancy = 0;
					leave = 0;
					flag = true;
				}else {
					record = (Record) getSession().createQuery(hql).list().get(0);
					late = record.getLate();
					early = record.getEarly();
					normal = record.getNormal();
					truancy = record.getTruancy();
					leave = record.getLeaved();
				}
				
				if(array.getJSONObject(i).getString("status").equals("0")) {
					normal = normal + 1;
				}else if(array.getJSONObject(i).getString("status").equals("1")) {
					truancy = truancy + 1;
				}else if(array.getJSONObject(i).getString("status").equals("2")) {
					late = late + 1;
				}else if(array.getJSONObject(i).getString("status").equals("3")) {
					leave = leave + 1;
				}else if(array.getJSONObject(i).getString("status").equals("4")) {
					early = early + 1;
				}
				
				record.setLate(late);
				record.setEarly(early);
				record.setNormal(normal);
				record.setTruancy(truancy);
				record.setLeaved(leave);
				record.setStudentNo(array.getJSONObject(i).getString("studentNo"));
				record.setClassNo(array.getJSONObject(i).getString("classNo"));
				record.setStudentName(array.getJSONObject(i).getString("studentName"));
				
				if(flag == true) {
					session.save(record);	
				}else {
					session.update(record);
				}
				
			}
//			session.flush();
//			session.clear();
			session.getTransaction().commit();
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println("==批量保存失败===============");
			session.getTransaction().rollback(); // 出错将回滚事物
		}finally {
			session.close();
		}
	}
	
	/**
	 * 批量删除考勤记录
	 * @param ids
	 * @return
	 */
	public boolean delete(String ids) {
		JSONArray array = JSONArray.fromObject(ids);

		Session session = null;
		session = getSession();
		
		boolean flag = true;
		
		session.beginTransaction();
		try {
			
			for(int i = 0; i < array.size(); i++) {
				Record record = new Record();
				
				record.setStudentNo(array.getJSONObject(i).getString("studentNo"));
				
				session.delete(record);
			}
			session.getTransaction().commit();
			
		}catch(Exception e) {
			flag = false;
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally {
			session.close();
		}
		
		return flag;
	}
	
	/**
	 * 根据当前教师查询全部的考勤记录
	 */
	@SuppressWarnings("unchecked")
	public List<Record> recordList(Record record, Page page) {
		List<Record> records = new ArrayList<Record>();
		
		// 获取当前用户类型
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String utype = (String) session.getAttribute("utype");
		User currentUser  = (User) session.getAttribute("user");
		
		String hql = "from Record where 1=1";
		if(record.getClassNo() != null && !"".equals(record.getClassNo())) {
			hql = hql + " and classNo = '" + record.getClassNo() + "' ";
		}
		
		if(record.getStudentName() != null && !"".equals(record.getStudentName())) {
			hql = hql + "and studentName like '%" + record.getStudentName() + "%'";
		}
		
		records = getSession().createQuery(hql).list();
		
		session.setAttribute("recordSize", records.size());
		// 分页
		if(page.getPage() * page.getRows() > records.size()) {
			records = records.subList(page.getOffset(), records.size());
		}else {
			records = records.subList(page.getOffset(), page.getPage() * page.getRows());
		}
		
		return records;
	}
}
